package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.TlGoods;
import com.qf.entity.TlGorder;
import com.qf.entity.TlGorderitem;
import com.qf.entity.TlUser;
import com.qf.mapper.GoodsMapper;
import com.qf.mapper.GorderItemMapper;
import com.qf.mapper.GorderMapper;
import com.qf.service.CartService;
import com.qf.service.GorderItemService;
import com.qf.service.GorderService;
import com.qf.service.UserService;
import com.qf.utils.Mycode;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class GorderServiceImpl extends ServiceImpl<GorderMapper, TlGorder> implements GorderService {

    @Autowired(required = false)
    UserService userService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired(required = false)
    GoodsMapper goodsMapper;

    @Autowired
    GorderItemService gorderItemService;

    @Autowired(required = false)
    GorderItemMapper gorderItemMapper;

    @Autowired(required = false)
    GorderMapper gorderMapper;

    @Autowired
    CartService cartService;

    @Override
    public ResultMsg findAll(HttpServletRequest request) {
        //获取登录用户的信息
        ResultMsg resultMsg = userService.user(request);
        TlUser user = (TlUser) resultMsg.getData();
        Integer uid = user.getUserId();
        QueryWrapper<TlGorder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",uid);
        List<TlGorder> list = super.list(wrapper);
        return ResultMsg.SUCCESS(list);
    }

    @Override
    public ResultMsg insert(Integer gid,Integer num, String addr, HttpServletRequest request) {
        //查询商品信息
        TlGoods tlGoods = goodsMapper.selectById(gid);
        //获取登录用户的信息
        ResultMsg resultMsg = userService.user(request);
        TlUser user = (TlUser) resultMsg.getData();
        ResultMsg data = findAll(request);
        Object data1 = data.getData();
        List<TlGorder> list = (List<TlGorder>) data1;
        int size = list.size()+1;
        // 生成 UUID
        UUID uuid1 = UUID.randomUUID();
        String uuidString = uuid1.toString();
        TlGorder tlGorder = new TlGorder();
        tlGorder.setGorderId(uuidString);
        tlGorder.setUserId(user.getUserId());
        tlGorder.setGorderName(user.getUserName()+"的第"+size+"笔订单");
        BigDecimal goodsPrice = tlGoods.getGoodsPrice().multiply(new BigDecimal(num));
        tlGorder.setGorderPrice(goodsPrice);
        tlGorder.setGorderAddr(addr);
        tlGorder.setGorderTime(new Date());
        tlGorder.setGorderFlag(0);//未支付
        //添加订单
        boolean save = super.saveOrUpdate(tlGorder);
        //商品库存-1
        tlGoods.setGoodsStock(tlGoods.getGoodsStock()-num);
        goodsMapper.updateById(tlGoods);
        //添加订单详情
        Integer[] goodsId = {gid};
        Integer[] number = {num};
        gorderItemService.insert(goodsId, uuidString, number);
        if (save)
            return ResultMsg.SUCCESS();
        return ResultMsg.ERROR(Mycode.GORDERINSERT_ERRO);
    }
    @Override
    public ResultMsg insertAll(Integer[] gid, Integer[] num, String addr, HttpServletRequest request) {
        //获取登录用户的信息
            ResultMsg resultMsg = userService.user(request);
            TlUser user = (TlUser) resultMsg.getData();
            ResultMsg data = findAll(request);
            Object data1 = data.getData();
            List<TlGorder> list = (List<TlGorder>) data1;
            int size = list.size()+1;
            // 生成 UUID
            UUID uuid1 = UUID.randomUUID();
            String uuidString = uuid1.toString();
            TlGorder tlGorder = new TlGorder();
            tlGorder.setGorderId(uuidString);
            tlGorder.setUserId(user.getUserId());
            tlGorder.setGorderName(user.getUserName()+"的第"+size+"次订单");
            Integer priceAll = 0;
            for (int i = 0; i < gid.length; i++) {
                TlGoods tlGoods = goodsMapper.selectById(gid[i]);
                priceAll += (tlGoods.getGoodsPrice().intValue()) * num[i];
                //商品库存-num
                tlGoods.setGoodsStock(tlGoods.getGoodsStock()-num[i]);
                goodsMapper.updateById(tlGoods);
                //删除购物车商品
                cartService.deleteOne(gid[i]);
            }
            BigDecimal goodsPrice = BigDecimal.valueOf(priceAll);
            tlGorder.setGorderPrice(goodsPrice);
            tlGorder.setGorderAddr(addr);
            tlGorder.setGorderTime(new Date());
            tlGorder.setGorderFlag(0);//未支付
            //添加订单
            boolean save = super.saveOrUpdate(tlGorder);
            //添加订单详情
            gorderItemService.insert(gid, uuidString, num);
        return ResultMsg.SUCCESS();
    }

    @Override
    public ResultMsg moveById(String gorderId) {
        TlGorder tlGorder = gorderMapper.selectById(gorderId);
        tlGorder.setGorderFlag(2);
        super.saveOrUpdate(tlGorder);
        ResultMsg resultMsg = gorderItemService.selectOne(gorderId);
        List<TlGorderitem> tlGorderitems = (List<TlGorderitem>) resultMsg.getData();
        //更改gorderItem中的订单
        for (int i = 0; i < tlGorderitems.size(); i++) {
            TlGorderitem tlGorderitem = tlGorderitems.get(i);
            //取消订单后 商品库存+num
            QueryWrapper<TlGoods> wrapper = new QueryWrapper<>();
            wrapper.eq("goods_name",tlGorderitem.getGorderitemName());
            TlGoods tlGoods = goodsMapper.selectOne(wrapper);

            QueryWrapper<TlGoods> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("goods_id",tlGoods.getGoodsId());
            Integer y = tlGoods.getGoodsStock();
            Integer x = tlGorderitem.getGorderitemNum();
            tlGoods.setGoodsStock(x+y);
            goodsMapper.update(tlGoods,wrapper1);
        }
        return ResultMsg.SUCCESS();

    }

    @Override
    public ResultMsg findOne(String gorderId,HttpServletRequest request) {
        //获取登录用户的信息
        ResultMsg resultMsg = userService.user(request);
        TlUser user = (TlUser) resultMsg.getData();
        TlGorder gorder = super.getById(gorderId);
        gorder.setName(user.getUserName());
        return ResultMsg.SUCCESS(gorder);
    }

    @Override
    public ResultMsg deleteById(String gorderId) {
        ResultMsg resultMsg = gorderItemService.selectOne(gorderId);
        List<TlGorderitem> tlGorderitems = (List<TlGorderitem>) resultMsg.getData();
        //删除gorderItem中的订单
        for (int i = 0; i < tlGorderitems.size(); i++) {
            TlGorderitem tlGorderitem = tlGorderitems.get(i);
            gorderItemMapper.deleteById(tlGorderitem);
        }
        //删除gorder中的订单
        gorderMapper.deleteById(gorderId);
        return ResultMsg.SUCCESS();
    }

    @Override
    public ResultMsg findAllByFlagZero(HttpServletRequest request) {
            //获取登录用户的信息
            ResultMsg resultMsg = userService.user(request);
            TlUser user = (TlUser) resultMsg.getData();
            Integer uid = user.getUserId();
            QueryWrapper<TlGorder> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",uid);
            wrapper.eq("gorder_flag",0);
            List<TlGorder> list = super.list(wrapper);
        return ResultMsg.SUCCESS(list);
    }

    @Override
    public ResultMsg findAllByFlagOne(HttpServletRequest request) {
        //获取登录用户的信息
        ResultMsg resultMsg = userService.user(request);
        TlUser user = (TlUser) resultMsg.getData();
        Integer uid = user.getUserId();
        QueryWrapper<TlGorder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",uid);
        wrapper.eq("gorder_flag",1);
        List<TlGorder> list = super.list(wrapper);
        return ResultMsg.SUCCESS(list);
    }

    @Override
    public ResultMsg findAllByFlagTwo(HttpServletRequest request) {
        //获取登录用户的信息
        ResultMsg resultMsg = userService.user(request);
        TlUser user = (TlUser) resultMsg.getData();
        Integer uid = user.getUserId();
        QueryWrapper<TlGorder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",uid);
        wrapper.eq("gorder_flag",2);
        List<TlGorder> list = super.list(wrapper);
        return ResultMsg.SUCCESS(list);
    }
}
